#!/user/bin/env python
# -*-coding:utf-8-*-
'''
| @author: Patty
| @project: automation_framework
| @file: qarule.py
| @time: 2020/3/11  14:16
| @IDE: PyCharm
'''

from util.db_util import execute_query
import logging


def get_qa_rule_def_id(name, stage):
    """根据质检名称与适用步骤查询质检规则id

    :param name: 质检名称
    :param stage: 质检适用步骤

    :return: 质检规则id
    """

    results = execute_query("SELECT id FROM `t_das_qa_rule_def` WHERE rule_name='%s' AND stage='%s';" % (name, stage))
    try:
        result = results[0][0]
        return result
    except Exception as e:
        logging.error("no result retrieved")
        return None


def get_qc_rule_def_info(ruleName, stage, args):
    """根据质检名称与适用步骤查询质检定义信息

    :param ruleName: 质检名称
    :param stage: 质检适用步骤
    :param args: 需获取的质检定义字段(如id,rule_type,qcgroup,item_kind)

    :return: 质检定义字段值
    """

    results = execute_query("SELECT %s FROM `t_das_qa_rule_def` WHERE rule_name='%s' AND stage='%s';" % (args, ruleName, stage))
    try:
        result = results[0][0]
        return result
    except Exception as e:
        logging.error("get_qc_rule_def_info: %s"%e)
        return None


def get_qc_rule_result_info(ruleName, stage, fileId, qcScope, args):
    """根据文件质检规则获取质检结果信息

    :param ruleName: 质检名称
    :param stage: 质检适用步骤
    :param fileId: 文件Id
    :param qcScope: 质检分组/模板Id
    :param args: 需获取的质检结果字段(如status,message)

    :return: 质检结果字段值
    """
    ruleId = get_qc_rule_def_info(ruleName, stage, "id")

    results = execute_query(
        "SELECT %s FROM `t_das_qa_element_result` WHERE rule_id='%s' AND file_id='%s' AND qcscope_id='%s';" % (
            args, ruleId, fileId, qcScope))
    try:
        result = results[0][0]
        return result
    except Exception as e:
        logging.error("get_qc_rule_result_info: %s" % e)
        return None


def get_qc_interperiod_rule_result_info(ruleName, stage, fileId, args):
    """根据文件质检规则获取质检结果信息

    :param ruleName: 质检名称
    :param stage: 质检适用步骤
    :param fileId: 文件Id
    :param args: 需获取的质检结果字段(如status,message)

    :return: 质检结果字段值
    """
    ruleId = get_qc_rule_def_info(ruleName, stage, "id")

    results = execute_query(
        "SELECT %s FROM `t_das_qc_interperiod_result` WHERE rule_id='%s' AND file_id='%s';" % (
            args, ruleId, fileId))
    try:
        result = results[0][0]
        return result
    except Exception as e:
        logging.error("get_qc_interperiod_rule_result_info: %s" % e)
        return None